#include <iostream>
#include <string.h>
using namespace std;
class String;
class String
{
public:
	String();
	String(const char *s);
	String(const String &s);
	~String(){if(m_str)delete m_str;};
	//ostream &operator <<(ostream &o);
	
	friend ostream &operator <<(ostream &o, String &s)
	{
		o<<s.m_str;
		return o;
	}
	String& operator=(const String &s)
	{
		if(s.m_str)
		{
			if(s.m_size>this->m_size)
			{
				if(this->m_str)
					delete this->m_str;
				this->m_str = new char[s.m_size];
				memcpy(this->m_str, s.m_str, s.m_size);
				*(this->m_str+s.m_size)='\0';
			}
			else
			{
				memcpy(this->m_str, s.m_str, s.m_size);
				*(this->m_str+s.m_size)='\0';
			}
		}
		return *this;
	}
private:
	char *m_str;
	size_t m_size;
};

String::String()
{
	m_size = 1;
	m_str = new char[1];
	memset(m_str, 0, m_size);
}
String::String(const char *s)
{
	if(s==NULL)
		m_str = new char[1];
	else
	{
	int size = strlen(s);
	if(m_size<size)
	{
		if(m_str!=NULL)	delete m_str;
		m_str = new char[size*2];
		memcpy(m_str, s, size);
		*(m_str+size) = '\0';
	}
	else
	{
		memcpy(m_str, s, size);	
		*(m_str+size) = '\0';
	}
	}
}
String::String(const String &s)
{
	if(this->m_size<s.m_size)
	{
		if(this->m_str)
		{
			delete this->m_str;
		}
		
		this->m_str = new char[s.m_size*2];
		memcpy(this->m_str, s.m_str, s.m_size);
		*(this->m_str+s.m_size)='\0';
	}
	else
	{
		memcpy(this->m_str, s.m_str, s.m_size);
		*(this->m_str+s.m_size)='\0';
	}	
}
int main()
{
	char str[10]={"hello"};
	String s(str);
	std::cout<<"String instance create succeed!"<<std::endl;
	std::cout<<s<<std::endl;
	String s1(NULL);
	std::cout<<s1<<std::endl;	
	s1 = s;
	std::cout<<s<<std::endl;
}
